package com.team.recruitment_management_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.team.recruitment_management_system.mapper.SysUserMapper;
import com.team.recruitment_management_system.pojo.entity.SysUser;
import com.team.recruitment_management_system.service.EmailService;
import com.team.recruitment_management_system.util.MailUtils;
import com.team.recruitment_management_system.util.result.Result;
import com.team.recruitment_management_system.util.result.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

import static com.team.recruitment_management_system.pojo.constant.EmailConstant.*;

@Service
public class EmailServiceImpl implements EmailService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private MailUtils mailUtils;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Result<Void> getLoginCaptcha(String email) {
        if (!isEmailExist(email)) {
            return Result.build(null, ResultCodeEnum.LOGIN_EMAIL_ERROR);
        }
        String captcha = mailUtils.getCode();
        redisTemplate.opsForValue().set(EMAIL_LOGIN_PRIM + email, captcha, 5L, TimeUnit.MINUTES);
        mailUtils.sendMail(email, "您的登录验证码为：" + captcha + " 五分钟之内有效", "招聘通验证码");
        return Result.ok(null, "邮件成功发送！");
    }

    @Override
    public Result<Void> getChangePwdCaptcha(String email) {
        if (!isEmailExist(email)) {
            return Result.build(null, ResultCodeEnum.LOGIN_EMAIL_ERROR);
        }
        String captcha = mailUtils.getCode();
        redisTemplate.opsForValue().set(EMAIL_CHANGE_PW_PRIM + email, captcha, 5L, TimeUnit.MINUTES);
        mailUtils.sendMail(email, "您的密码修改验证码为：" + captcha + " 五分钟之内有效", "招聘通验证码");
        return Result.ok(null, "邮件成功发送！");
    }

    @Override
    public Result<Void> getOldEmailCaptcha(String email) {
        if (!isEmailExist(email)) {
            return Result.build(null, ResultCodeEnum.LOGIN_EMAIL_ERROR);
        }
        String captcha = mailUtils.getCode();
        redisTemplate.opsForValue().set(EMAIL_CHANGE_EM_OLD_PRIM + email, captcha, 5L, TimeUnit.MINUTES);
        mailUtils.sendMail(email, "您的旧邮箱验证码为：" + captcha + " 五分钟之内有效", "招聘通验证码");
        return Result.ok(null, "邮件成功发送！");
    }

    @Override
    public Result<Void> getNewEmailCaptcha(String email) {
        if (isEmailExist(email)) {
            return Result.build(null, ResultCodeEnum.EMAIL_USED);
        }
        String captcha = mailUtils.getCode();
        redisTemplate.opsForValue().set(EMAIL_CHANGE_EM_NEW_PRIM + email, captcha, 5L, TimeUnit.MINUTES);
        mailUtils.sendMail(email, "您的新邮箱验证码为：" + captcha + " 五分钟之内有效", "招聘通验证码");
        return Result.ok(null, "邮件成功发送！");
    }

    @Override
    public Result<Void> getEmailBindingCaptcha(String email) {
        if (isEmailExist(email)) {
            return Result.build(null, ResultCodeEnum.EMAIL_USED);
        }
        String captcha = mailUtils.getCode();
        redisTemplate.opsForValue().set(EMAIL_BINDING_EM_PRIM + email, captcha, 5L, TimeUnit.MINUTES);
        mailUtils.sendMail(email, "您的绑定邮箱验证码为：" + captcha + " 五分钟之内有效", "招聘通验证码");
        return Result.ok(null, "邮件成功发送！");
    }

    private boolean isEmailExist(String email) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getEmail, email);
        return sysUserMapper.exists(queryWrapper);
    }

}
